﻿using System;
using System.Windows;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace Waos
{
    public partial class Form1 : Form
    {
        TextBox t;
        List<Note> list = new List<Note>();
        Label l = new Label();
        NotifyIcon ni = new NotifyIcon();
        FontDialog fd = new FontDialog();
        Font ff = new Font(FontFamily.GenericSerif, 20);
        Button b = new Button();
        Color col = new Color();

        public Form1()
        {
            InitializeComponent();
            ni.Visible = false;
            IFormatter formatter = new BinaryFormatter();
            Stream readStream = new FileStream("Person.bin", FileMode.Open, FileAccess.Read, FileShare.Read);
            list = (List<Note>)formatter.Deserialize(readStream);
            readStream.Close();
            foreach (Note n in list)
            {
                l = new Label();
                l.Font = ff;
                l.Margin = new Padding(0);
               
                l.Left = (int)n.X;
                l.Top = (int)n.Y;
                l.Text = n.Text;
                l.AutoSize = true;
                this.Controls.Add(l);
            }

            l.ContextMenu = new ContextMenu();
            l.ContextMenu.MenuItems.Add(new MenuItem());
            l.ContextMenu.MenuItems[0].Text = "Erase";
            l.ContextMenu.MenuItems[0].Click += new EventHandler(eraseLabel);

            this.ContextMenu = new ContextMenu();
            this.ContextMenu.MenuItems.Add(new MenuItem());
            this.ContextMenu.MenuItems[0].Text = "Erase all";
            this.ContextMenu.MenuItems[0].Click += new EventHandler(eraseAll);

            //adds the change.font button
            Button bChangeFont = new Button();
            bChangeFont.Text = "A";
            bChangeFont.BackColor = Color.Black;
            bChangeFont.ForeColor = Color.White;
            bChangeFont.Font = new Font(FontFamily.GenericSerif, 30);
            bChangeFont.AutoSize = true;
            bChangeFont.FlatStyle = FlatStyle.Flat;
            bChangeFont.FlatAppearance.BorderSize = 0;
            bChangeFont.Left = Screen.PrimaryScreen.Bounds.Width - bChangeFont.Width;
            bChangeFont.Top = bChangeFont.Height * 4;
            bChangeFont.Click += new EventHandler(openFontDialog);
            bChangeFont.KeyDown += new KeyEventHandler(Form1_KeyDown);
            this.Controls.Add(bChangeFont);

            //adds the change.colour button
            Button bChangeColour = new Button();
            bChangeColour.BackColor = Color.Black;
            bChangeColour.BackgroundImage = Image.FromFile("changeColor.png");
            bChangeColour.BackgroundImageLayout = ImageLayout.Center;
            bChangeColour.ForeColor = Color.White;
            bChangeColour.Font = new Font(FontFamily.GenericSerif, 30);
            bChangeColour.Left = (int)((Screen.PrimaryScreen.Bounds.Width - bChangeColour.Width));
            bChangeColour.Top = (int)(1.8 * bChangeFont.Top);
            bChangeColour.Width = bChangeFont.Width;
            bChangeColour.KeyDown += new KeyEventHandler(Form1_KeyDown);
            bChangeColour.Height = bChangeFont.Height;
            bChangeColour.FlatStyle = FlatStyle.Flat;
            bChangeColour.FlatAppearance.BorderSize = 0;
            bChangeColour.Click += new EventHandler(openColorDialog);
            this.Controls.Add(bChangeColour);

        }

        void eraseAll(object sender, EventArgs e)
        {
            list.Clear();
            this.Controls.Clear();
        }

        void openColorDialog(object sender, EventArgs e)
        {
            ColorDialog cd = new ColorDialog();
            if (cd.ShowDialog() == DialogResult.OK)
                col = cd.Color;
        }


        void eraseLabel(object sender, EventArgs e)
        {
            Note nt = new Note("", 0, 0);
            foreach (Note n in list)
                if (n.Text == ((Label)((ContextMenu)(((MenuItem)sender).GetContextMenu())).SourceControl).Text) nt = n;
            list.Remove(nt);
            this.Controls.Remove(((Label)((ContextMenu)(((MenuItem)sender).GetContextMenu())).SourceControl));

        }
        const int width = 10;

        private void t_TextChanged(object sender, EventArgs e)
        {
            Graphics g = Graphics.FromHwnd(t.Handle);
            SizeF sf = g.MeasureString(t.Text, t.Font);
            t.Width = (int)sf.Width;
            t.Height = (int)sf.Height;
        }

        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {
            t = new TextBox();
            

            t.Multiline = true;
            t.Margin = new Padding(0);
            t.TextChanged += new EventHandler(t_TextChanged);
            t.BackColor = Color.Black;
            t.BorderStyle = BorderStyle.None;
            t.Font = ff;
            t.ForeColor = col;
            t.Left = MousePosition.X;
            t.Top = MousePosition.Y;
            t.Text = "";
            t.KeyDown += new KeyEventHandler(keyPressed);
            t.Leave += new EventHandler(lostFocus);
            this.Controls.Add(t);
            t.Select();
        }

        void openFontDialog(object sender, EventArgs e)
        {
            fd = new FontDialog();
            if (fd.ShowDialog() == DialogResult.OK)
                ff = fd.Font;

        }

        void keyPressed(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                l = new Label();
                l.Font = ff;
                l.ForeColor = col;
                l.Left = t.Left;
                l.Margin = new Padding(0);
                l.Top = t.Top;
                l.Text = t.Text;
                l.AutoSize = true;
                l.MouseEnter += new EventHandler(showErase);
                this.Controls.Add(l);
                list.Add(new Note(l.Text, l.Left, l.Top));
                this.Controls.Remove(t);
            }
        }
        Button bb = new Button();
        void showErase(object sender, EventArgs e)
        {
            
            bb.Text = "Erase";
            this.Controls.Add(bb);
            Timer timer = new Timer();

            timer.Enabled = true;
            timer.Interval = 5000;
            
            timer.Tick += new System.EventHandler(this.timer1_Tick);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Controls.Remove(bb);
        }

        void lostFocus(object sender, EventArgs e)
        {
            l = new Label();
            l.Font = ff;
            l.ForeColor = col;
            l.Left = t.Left;
            l.Top = t.Top;
            l.Text = t.Text;
            l.AutoSize = true;
            this.Controls.Add(l);
            this.Controls.Remove((TextBox)sender);
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                IFormatter formatter = new BinaryFormatter();
                Stream stream = new FileStream("Person.bin", FileMode.Create, FileAccess.Write, FileShare.None);
                formatter.Serialize(stream, list);
                stream.Close();

                ni.ShowBalloonTip(5000, "Info", "Click here to write!", ToolTipIcon.Info);
                ni.Icon = new Icon("icon.ico");
                ni.Visible = true;
                this.WindowState = FormWindowState.Minimized;
                ni.Click += new EventHandler(open);
                this.Hide();
            }
        }

        void open(object sender, EventArgs e)
        {
            this.Show();
            this.WindowState = FormWindowState.Maximized;
            this.Activate();
        }
    }
}
